package com.mercury.elasticsearch.service.impl;

import com.mercury.elasticsearch.factory.EsProductRepository;
import com.mercury.elasticsearch.model.EsProduct;
import com.mercury.elasticsearch.service.EsProductService;
import lombok.extern.slf4j.Slf4j;
import org.elasticsearch.common.lucene.search.function.FunctionScoreQuery;
import org.elasticsearch.index.query.QueryBuilders;
import org.elasticsearch.index.query.functionscore.FunctionScoreQueryBuilder;
import org.elasticsearch.index.query.functionscore.ScoreFunctionBuilders;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.data.elasticsearch.core.SearchHit;
import org.springframework.data.elasticsearch.core.SearchHits;
import org.springframework.data.elasticsearch.core.query.NativeSearchQuery;
import org.springframework.data.elasticsearch.core.query.NativeSearchQueryBuilder;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;
import java.util.stream.Collectors;

/**
 * @author mercury
 */
@Service
@Slf4j
public class EsProductServiceImpl implements EsProductService {

    @Resource
    private ElasticsearchRestTemplate elasticsearchRestTemplate;

    @Resource
    private EsProductRepository esProductRepository;

    @Override
    public void saveDocument() {
        EsProduct esProduct = new EsProduct();
        esProduct.setBrandName("zc");
        esProduct.setKeywords("李四");
        esProductRepository.save(esProduct);
    }

    @Override
    public void removeDocument(String id) {

        esProductRepository.deleteById(id);
    }

    @Override
    public void findByKeyWords(String keywords) {
        Pageable pageable = PageRequest.of(0, 10);
        Page<EsProduct> byKeywords = esProductRepository.findByKeywords(keywords, pageable);
        List<EsProduct> content = byKeywords.getContent();

    }

    @Override
    public void findByConditions(String keyword) {
        NativeSearchQueryBuilder nativeSearchQueryBuilder = new NativeSearchQueryBuilder();



        Pageable pageable = PageRequest.of(0, 10);
        //构建分页参数
        nativeSearchQueryBuilder.withPageable(pageable);

        List<FunctionScoreQueryBuilder.FilterFunctionBuilder> filterFunctionBuilders = new ArrayList<>();
        //构造查询条件 之后指定查询权重
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("brandName", keyword),
                ScoreFunctionBuilders.weightFactorFunction(5)));
        filterFunctionBuilders.add(new FunctionScoreQueryBuilder.FilterFunctionBuilder(QueryBuilders.matchQuery("keywords", keyword),
                ScoreFunctionBuilders.weightFactorFunction(6)));

        FunctionScoreQueryBuilder.FilterFunctionBuilder[] builders = new FunctionScoreQueryBuilder.FilterFunctionBuilder[filterFunctionBuilders.size()];
        filterFunctionBuilders.toArray(builders);
        FunctionScoreQueryBuilder functionScoreQueryBuilder = QueryBuilders.functionScoreQuery(builders)
                .scoreMode(FunctionScoreQuery.ScoreMode.SUM)
                .setMinScore(2);
        //封装查询对象
        nativeSearchQueryBuilder.withQuery(functionScoreQueryBuilder);
        NativeSearchQuery searchQuery = nativeSearchQueryBuilder.build();
        //查询数据
        SearchHits<EsProduct> searchHits = elasticsearchRestTemplate.search(searchQuery, EsProduct.class);

        List<EsProduct> collect = searchHits.stream().map(SearchHit::getContent).collect(Collectors.toList());

    }


}
